package com.mail.manager.modular.controller.portal;

import com.alibaba.fastjson.JSONObject;
import com.mail.manager.common.annotion.log.BussinessLog;
import com.mail.manager.common.constant.Dict;
import com.mail.manager.common.controller.BaseController;
import com.mail.manager.common.exception.BizExceptionEnum;
import com.mail.manager.common.exception.BussinessException;
import com.mail.manager.config.properties.GunsProperties;
import com.mail.manager.core.log.LogObjectHolder;
import com.mail.manager.core.shiro.ShiroKit;
import com.mail.manager.core.util.ToolUtil;
import com.mail.manager.modular.mapper.NewsMapper;
import com.mail.manager.modular.model.News;
import com.mail.manager.modular.service.portal.INewsService;
import com.mail.manager.modular.warpper.NoticeWrapper;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 通知控制器
 *
 * @author fengshuonan
 * @Date 2017-05-09 23:02:21
 */
@Controller
@RequestMapping("/portal/news")
public class NewsController extends BaseController {

    private String PREFIX = "/portal/news/";

    @Resource
    private GunsProperties gunsProperties;

    @Resource
    private INewsService newsService;

    /**
     * 跳转到通知列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "news.html";
    }

    /**
     * 跳转到添加通知
     */
    @RequestMapping("/news_add")
    public String newsAdd() {
        return PREFIX + "news_add.html";
    }

    /**
     * 跳转到修改通知
     */
    @RequestMapping("/news_update/{newsId}")
    public String newsUpdate(@PathVariable Integer newsId, Model model) {
        News news = newsService.selectByPrimaryKey(newsId);
        model.addAttribute("news",news);
        LogObjectHolder.me().set(news);
        return PREFIX + "news_edit.html";
    }

    /**
     * 跳转到首页通知
     */
    @RequestMapping("/hello")
    public String hello() {
        List<Map<String, Object>> newss = newsService.list(null);
        super.setAttr("newsList",newss);
        return "/blackboard.html";
    }

    /**
     * 获取通知列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = newsService.list(condition);
        return super.warpObject(new NoticeWrapper(list));
    }

    /**
     * 新增通知
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(News news) {
        if (ToolUtil.isOneEmpty(news, news.getTitle(), news.getContent())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        news.setCreater(ShiroKit.getUser().getId());
        news.setCreate_time(new Date());
        String description = news.getContent()
                .replaceAll("& lt;", "<")
                .replaceAll("& gt;", ">");
        news.setContent(description);
        newsService.insert(news);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除通知
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer newsId) {
        newsService.delteObject(newsId);
        return SUCCESS_TIP;
    }

    /**
     * 修改通知
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(News news) {
        if (ToolUtil.isOneEmpty(news, news.getId(), news.getTitle(), news.getContent())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String description = news.getContent()
                .replaceAll("& lt;", "<")
                .replaceAll("& gt;", ">");
        news.setContent(description);
        newsService.updateObjtById(news);
        return super.SUCCESS_TIP;
    }

    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ResponseBody
    public JSONObject upload(@RequestPart("file") MultipartFile picture) {
        String pictureName = UUID.randomUUID().toString() + ".jpg";
        JSONObject result = new JSONObject();
        String path = gunsProperties.getFileUploadPath()+"/news/" + pictureName;
        Client client = new Client();
        WebResource resource = client.resource(path);
        try {
            resource.put(String.class, picture.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        result.put("errno", 0);
        result.put("data", path);
        return result;
    }

}
